package com.srd.phone.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.shengrongdai.srd.R;

/**
 * 借款  fragment
 */
public class TabBorrowFragment extends BaseFragment implements RadioGroup.OnCheckedChangeListener {

    private View viewRoot;
    private RadioGroup rg;
    private FrameLayout layout_fragment1;
    private FrameLayout layout_fragment2;

    private boolean isLoading = false;
    private TabBorrowPeopleFragment fragment1;
    private TabBorrowEnterpriseFragment fragment2;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (viewRoot == null) {
            viewRoot = inflater.inflate(R.layout.fragment_tab_borrow, container, false);
            viewRoot.findViewById(R.id.iv_common_back).setVisibility(View.GONE);
            ((TextView) viewRoot.findViewById(R.id.tv_common_title)).setText("借款");
        }
        return viewRoot;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isLoading == false && isVisibleToUser == true) {
            isLoading = true;
            initView();
        }
    }

    @Override
    public void initView() {
        super.initView();
        fragment1 = new TabBorrowPeopleFragment();
        fragment2 = new TabBorrowEnterpriseFragment();
        layout_fragment1 = (FrameLayout) viewRoot.findViewById(R.id.layout_fragment1);
        layout_fragment2 = (FrameLayout) viewRoot.findViewById(R.id.layout_fragment2);
        replaceFragment(R.id.layout_fragment1, fragment1);
        replaceFragment(R.id.layout_fragment2, fragment2);
        rg = (RadioGroup) viewRoot.findViewById(R.id.rg_borrow);
        rg.setOnCheckedChangeListener(this);
        rg.check(R.id.rb_borrow_people);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId) {
            case R.id.rb_borrow_people:
                layout_fragment1.setVisibility(View.VISIBLE);
                layout_fragment2.setVisibility(View.GONE);
                break;
            case R.id.rb_borrow_enterprise:
                layout_fragment1.setVisibility(View.GONE);
                layout_fragment2.setVisibility(View.VISIBLE);
                break;
        }
    }
}
